//
//  EarnUserView.swift
//  WalkNovel
//
//  Created by Kevin on 2023/10/6.
//

import UIKit

class EarnUserView: UIView {
    
    lazy var taskName: UILabel = {
        let label = UILabel()
        label.font = .wn_font(name: .bold, size: 18)
        label.textColor = .black
        label.text = "用戶任務"
        return label
    }()
    
    var taskClosure: AnyClosure<TaskModel>?
    
    var userTaskArr = [TaskModel]() {
        didSet {
            isHidden = userTaskArr.count == 0
            for item in subviews {
                if item.isKind(of: EarnDailyTaskView.self) || item.isKind(of: UserGiftView.self) || item.isKind(of: UserTimeView.self) {
                    item.removeFromSuperview()
                }
            }
            if userTaskArr.count > 0 {
                for (i, v) in userTaskArr.enumerated() {
                    if i == 0 {
                        let task = EarnDailyTaskView()
                        addSubview(task)
                        task.snp.makeConstraints { make in
                            make.top.equalTo(42)
                            make.left.right.equalTo(0)
                            make.height.equalTo(77)
                        }
                        task.model = v
                        task.taskClosure = { [weak self] m in
                            self?.taskClosure?(m)
                        }
//                    }else if i == 1 {
//                        let giftTask = UserGiftView()
//                        addSubview(giftTask)
//                        giftTask.snp.makeConstraints { make in
//                            make.top.equalTo(42+77)
//                            make.left.right.equalTo(0)
//                            make.height.equalTo(135)
//                        }
//                        giftTask.model = v
//                        giftTask.taskClosure = { [weak self] task in
//                            self?.taskClosure?(task)
//                        }
                    }else if i == 1 {
                        let timeTask = UserTimeView()
                        addSubview(timeTask)
                        timeTask.snp.makeConstraints { make in
                            make.top.equalTo(42+77) //135
                            make.left.right.equalTo(0)
                            make.height.equalTo(174)
                        }
                        timeTask.model = v
                        timeTask.taskClosure = { [weak self] m in
                            self?.taskClosure?(m)
                        }
                    }
                }
            }
            layoutIfNeeded()
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .white
        addSubview(taskName)
        taskName.snp.makeConstraints { make in
            make.top.equalTo(18)
            make.left.equalTo(12)
            make.height.equalTo(20)
        }
        wn_cornerWithBorder(8, 0.1, .white, 1, .init(white: 0, alpha: 0.1), .zero, 8, 1)
        layer.masksToBounds = false
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class UserGiftView: UIView {
    
    lazy var taskName: UILabel = {
        let label = UILabel()
        label.font = .wn_font(name: .bold, size: 14)
        label.textColor = .init(named: "black_333333")
        return label
    }()
    
    lazy var taskDes: UILabel = {
        let label = UILabel()
        label.font = .wn_font(name: .bold, size: 12)
        label.textColor = .init(named: "gray_666666")
        label.numberOfLines = 0
        return label
    }()
    
    lazy var slider: WNSlider = {
        var slider = WNSlider()
        slider.w = 0
        slider.minimumValue = 0
        slider.maximumValue = 1
        slider.setThumbImage(UIImage(), for: .normal)
        slider.minimumTrackTintColor = .init(named: "red_FE95B4")
        slider.maximumTrackTintColor = .init(named: "gray_ECE0E4")
        slider.backgroundColor = .clear
        slider.isEnabled = false
        return slider
    }()
    
    lazy var gift: UserGiftItemView = {
        let view = UserGiftItemView()
        return view
    }()
    
    lazy var gift1: UserGiftItemView = {
        let view = UserGiftItemView()
        view.taskClosure = { [weak self] m in
            self?.taskClosure?(m)
        }
        return view
    }()
    
    lazy var gift2: UserGiftItemView = {
        let view = UserGiftItemView()
        view.taskClosure = { [weak self] m in
            self?.taskClosure?(m)
        }
        return view
    }()
    
    var taskClosure: AnyClosure<TaskModel>?

    var model: TaskModel? {
        didSet {
            if let m = model {
                taskName.text = m.taskName
                if let arr = m.children, arr.count > 1 {
                    gift.model = m
                    gift1.model = arr[0]
                    gift2.model = arr[1]
                    taskDes.text = arr[1].desc
                    let v = Float(arr[1].cost)/Float(arr[1].accrueNum)
                    slider.value = v > 1 ? 1 : v
                }
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .white
        addSubview(taskName)
        addSubview(taskDes)
        addSubview(slider)
        addSubview(gift)
        addSubview(gift1)
        addSubview(gift2)
        taskName.snp.makeConstraints { make in
            make.top.equalTo(12)
            make.left.equalTo(12)
        }
        taskDes.snp.makeConstraints { make in
            make.top.equalTo(taskName.snp.bottom).offset(6)
            make.left.equalTo(taskName.snp.left)
            make.width.equalTo(120)
        }
        slider.snp.makeConstraints { make in
            make.bottom.equalTo(-23)
            make.left.equalTo(15)
            make.right.equalTo(-15)
            make.height.equalTo(2)
        }
        gift.snp.makeConstraints { make in
            make.left.equalTo(slider.snp.left).offset(0)
            make.bottom.equalTo(0)
            make.height.equalTo(98)
            make.width.equalTo(48)
        }
        gift1.snp.makeConstraints { make in
            make.centerX.equalToSuperview().offset(3)
            make.bottom.equalTo(0)
            make.height.equalTo(98)
            make.width.equalTo(48)
        }
        gift2.snp.makeConstraints { (make) in
            make.bottom.equalTo(0)
            make.right.equalTo(slider.snp.right).offset(3)
            make.height.equalTo(98)
            make.width.equalTo(48)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class UserGiftItemView: UIView {
    
    lazy var goldLabel: UILabel = {
        let label = UILabel()
        label.font = .wn_font(name: .bold, size: 12)
        label.textColor = .init(named: "red_FFB8C7")
        label.textAlignment = .center
        return label
    }()
    
    lazy var goldImage: UIImageView = {
        return UIImageView(image: UIImage(named: "reward_coin"))
    }()

    lazy var bookImage: UIButton = {
        let btn = UIButton(type: .custom)
        btn.imageView?.contentMode = .scaleAspectFill
        btn.rx.tap.bind { [weak self] in
            if self?.model?.status == 1 {
                WNHUD.wn_show("免費解鎖書籍：\n\(self?.model?.name ?? "")", 2)
            }else {
                self?.taskClosure?(self?.model ?? TaskModel())
            }
        }.disposed(by: rx.disposeBag)
        btn.wn_cornerWithBorder(2)
        return btn
    }()
    
    lazy var lockImage: UIImageView = {
        let image = UIImageView()
        return image
    }()
    
    lazy var redView: UIView = {
        let view = UIView()
        view.backgroundColor = .init(named: "red_FE95B4")
        view.wn_cornerWithBorder(4)
        return view
    }()
    
    var taskClosure: AnyClosure<TaskModel>?

    var model: TaskModel? {
        didSet {
            if let m = model {
                bookImage.isHidden = m.status == 0
                lockImage.isHidden = m.status == 0
                goldImage.isHidden = m.status == 0
                redView.isHidden = m.status == 0
                goldLabel.text = m.status == 0 ? "當前\(m.cost)" : "\(m.accrueNum)"
                bookImage.wn_setupImg(m.cover, placeHolder: "placeholder_cover_ver")
                if m.status == 1 {
                    lockImage.image = UIImage(named: "task_unlock")
                }else if m.status == 2 {
                    lockImage.image = UIImage(named: "task_lock")
                }else if m.status == 3 {
                    lockImage.image = UIImage(named: "library_book_sel")
                }
            }
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .clear
        addSubview(bookImage)
        bookImage.addSubview(lockImage)
        addSubview(goldLabel)
        addSubview(goldImage)
        addSubview(redView)
        bookImage.snp.makeConstraints { (make) in
            make.top.equalTo(0)
            make.width.equalTo(48)
            make.height.equalTo(68)
            make.centerX.equalToSuperview()
        }
        lockImage.snp.makeConstraints { (make) in
            make.center.equalToSuperview()
            make.width.height.equalTo(22)
        }
        goldLabel.snp.makeConstraints { make in
            make.centerX.equalTo(bookImage.snp.centerX)
            make.bottom.equalTo(0)
        }
        goldImage.snp.makeConstraints { (make) in
            make.centerY.equalTo(goldLabel.snp.centerY)
            make.left.equalTo(goldLabel.snp.right).offset(2)
            make.width.height.equalTo(14)
        }
        redView.snp.makeConstraints { (make) in
            make.right.equalTo(0)
            make.bottom.equalTo(-18)
            make.width.height.equalTo(8)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class UserTimeView: UIView {
    
    lazy var taskName: UILabel = {
        let label = UILabel()
        label.font = .wn_font(name: .bold, size: 14)
        label.textColor = .init(named: "black_333333")
        return label
    }()
    
    lazy var slider: WNSlider = {
        var slider = WNSlider()
        slider.w = 0
        slider.minimumValue = 0
        slider.maximumValue = 1
        slider.setThumbImage(UIImage(), for: .normal)
        slider.minimumTrackTintColor = .init(named: "red_FE95B4")
        slider.maximumTrackTintColor = .init(named: "gray_ECE0E4")
        slider.backgroundColor = .clear
        slider.isEnabled = false
        return slider
    }()
    
    lazy var time1: UserTimeItemView = {
        let view = UserTimeItemView()
        return view
    }()
    
    lazy var time2: UserTimeItemView = {
        let view = UserTimeItemView()
        return view
    }()
    
    lazy var time3: UserTimeItemView = {
        let view = UserTimeItemView()
        return view
    }()
    
    lazy var time4: UserTimeItemView = {
        let view = UserTimeItemView()
        return view
    }()
    
    lazy var taskBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setTitleColor(.white, for: .normal)
        btn.titleLabel?.font = .wn_font(name: .bold, size: 14)
        btn.rx.tap.bind { [weak self] in
            self?.handleBtn()
        }.disposed(by: rx.disposeBag)
        btn.wn_cornerWithBorder(16)
        return btn
    }()
    
    var taskClosure: AnyClosure<TaskModel>?
    
    var model: TaskModel? {
        didSet {
            if let m = model {
                taskName.text = "閱讀時長獎勵"
                slider.value = m.cost > 6000 ? 1 : Float(m.cost)/Float(6000)
                if let arr = m.children, arr.count > 3 {
                    time1.model = arr[0]
                    time2.model = arr[1]
                    time3.model = arr[2]
                    time4.model = arr[3]
                }
                if m.status == 1 {
                    taskBtn.isEnabled = true
                    taskBtn.setTitle("立即閱讀", for: .normal)
                    taskBtn.backgroundColor = .init(named: "red_FFB8C7")
                }else if m.status == 2 {
                    taskBtn.isEnabled = true
                    taskBtn.setTitle("領取", for: .normal)
                    taskBtn.backgroundColor = .init(named: "red_FE6390")
                }else if m.status == 3 {
                    taskBtn.isEnabled = false
                    taskBtn.setTitle("完畢", for: .normal)
                    taskBtn.backgroundColor = .init(named: "gray_E0E8EE")
                }
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .white
        addSubview(taskName)
        addSubview(taskBtn)
        addSubview(slider)
        addSubview(time1)
        addSubview(time2)
        addSubview(time3)
        addSubview(time4)
        taskName.snp.makeConstraints { make in
            make.top.equalTo(12)
            make.left.equalTo(12)
        }
        taskBtn.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.bottom.equalToSuperview().offset(-4)
            make.size.equalTo(CGSize.init(width: 160, height: 32))
        }
        slider.snp.makeConstraints { make in
            make.bottom.equalTo(taskBtn.snp.top).offset(-38)
            make.left.equalTo(15)
            make.right.equalTo(-15)
            make.height.equalTo(2)
        }
        time1.snp.makeConstraints { make in
            make.left.equalTo(slider.snp.left).offset((ScreenW-60)*0.1-27)
            make.centerY.equalTo(slider.snp.centerY).offset(-11)
            make.height.equalTo(70)
            make.width.equalTo(54)
        }
        time2.snp.makeConstraints { (make) in
            make.left.equalTo(time1.snp.left).offset((ScreenW-60)*0.2)
            make.centerY.equalTo(slider.snp.centerY).offset(-11)
            make.height.equalTo(70)
            make.width.equalTo(54)
        }
        time3.snp.makeConstraints { (make) in
            make.left.equalTo(time2.snp.left).offset((ScreenW-60)*0.3)
            make.centerY.equalTo(slider.snp.centerY).offset(-11)
            make.height.equalTo(70)
            make.width.equalTo(54)
        }
        time4.snp.makeConstraints { (make) in
            make.left.equalTo(time3.snp.left).offset((ScreenW-60)*0.3)
            make.centerY.equalTo(slider.snp.centerY).offset(-11)
            make.height.equalTo(70)
            make.width.equalTo(54)
        }
    }
    
    func handleBtn() {
        if let m = model {
            if m.status == 2 {
                if let arr = m.children, arr.count > 3 {
                    guard let cm = arr.filter({$0.status == 2}).first else { return }
                    taskClosure?(cm)
                }
            }else if m.status == 1 {
                viewContainingController?.navigationController?.popToRootViewController(animated: true)
                wn_getTopVC()?.tabBarController?.tabBar.isHidden = false
                WNThread.wn_delay(0.1) {
                    wn_getTopVC()?.tabBarController?.selectedIndex = 0
                }
            }
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class UserTimeItemView: UIView {
    
    lazy var goldLabel: UILabel = {
        let label = UILabel()
        label.font = .wn_font(name: .bold, size: 10)
        label.textColor = .init(named: "red_FFB8C7")
        label.textAlignment = .center
        return label
    }()
    
    lazy var goldImage: UIImageView = {
        return UIImageView(image: UIImage(named: "reward_coin"))
    }()
    
    lazy var timeLabel: UILabel = {
        let label = UILabel()
        label.font = .wn_font(name: .bold, size: 10)
        label.textColor = .init(named: "gray_999999")
        label.textAlignment = .center
        return label
    }()
    
    lazy var redView: UIView = {
        let view = UIView()
        view.backgroundColor = .init(named: "red_FE95B4")
        view.wn_cornerWithBorder(4)
        return view
    }()
    
    var model: TaskModel? {
        didSet {
            if let m = model {
                timeLabel.text = m.desc
                goldLabel.text = "+\(m.gold)"
                if m.status == 1 {
                    goldImage.image = UIImage(named: "reward_coin")
                }else if m.status == 2 {
                    goldImage.image = UIImage(named: "reward_coin")
                }else if m.status == 3 {
                    goldImage.image = UIImage(named: "library_book_sel")
                }
            }
        }
    }
        
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .clear
        addSubview(goldImage)
        addSubview(goldLabel)
        addSubview(timeLabel)
        addSubview(redView)
        goldImage.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(0)
            make.width.height.equalTo(20)
        }
        goldLabel.snp.makeConstraints { make in
            make.centerX.equalTo(goldImage.snp.centerX)
            make.top.equalTo(goldImage.snp.bottom).offset(4)
        }
        redView.snp.makeConstraints { (make) in
            make.centerX.equalTo(goldImage.snp.centerX)
            make.bottom.equalTo(-18)
            make.width.height.equalTo(8)
        }
        timeLabel.snp.makeConstraints { (make) in
            make.centerX.equalTo(goldImage.snp.centerX)
            make.bottom.equalTo(0)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
